import Shop.Shop;
import UserService.User;
import car.UsercarImp;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(name="addServlet",value = "/addServlet")//路径的定向
public class addServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String submit = req.getParameter("submit");
//        if (submit!=null){
//            resp.sendRedirect("Pay.jsp");
//        }
        int i=0;
        String button=null;
        while (button==null){
            button=req.getParameter("add"+i);
            i++;
        }
        i--;
        HttpSession session1= req.getSession();
        Shop shop= (Shop) session1.getAttribute("add"+i);//获取界面储存的shop



        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!！
        User user= (User) session1.getAttribute("user");


        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        UsercarImp ucar=new UsercarImp();
        int yezi=ucar.findcarquantity(shop.getProduct(),user.getUname(),shop.getManage());
        yezi++;
        if (yezi==1){
            ucar.addcart(user.getUname(),shop.getManage(),shop.getProduct(),yezi,shop.getPrice(),shop.getUrl());
        }else if(yezi>1){
            ucar.addupdate(user.getUname(),shop.getManage(),shop.getProduct(),yezi);
        }
//        ucar.addcart(user.getUname(),shop.manage,shop.product,yezi, shop.price, shop.url);

        System.out.println("添加成功!!!");

        // 重定向到商品页面
//        resp.sendRedirect("/mavenPro000_war_exploded/allshows-hop");
        resp.sendRedirect("car.jsp");

    }
}

